{extend name="common/layout" /}
{block name="title"}容灾切换策略{/block}
{block name="main"}
<style>
tbody tr>td:nth-child(2){overflow: hidden;text-overflow: ellipsis;white-space: nowrap;max-width:180px;}
</style>
<div class="row">
	<div class="col-xs-12 center-block" style="float: none;">
	<div class="panel panel-default panel-intro">
	<div class="panel-body">
	
<form onsubmit="return searchSubmit()" method="GET" class="form-inline" id="searchToolbar">
  <div class="form-group">
	<label>搜索</label>
	<div class="form-group">
		<select name="type" class="form-control"><option value="1">域名</option><option value="3">解析记录</option><option value="4">备用解析记录</option><option value="2">解析记录ID</option><option value="5">备注</option></select>
	  </div>
  </div>
  <div class="form-group">
	<input type="text" class="form-control" name="kw" placeholder="">
  </div>
  <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
  <a href="javascript:searchClear()" class="btn btn-default" title="刷新域名账户列表"><i class="fa fa-refresh"></i> 刷新</a>
  <a href="/dmonitor/task/add" class="btn btn-success"><i class="fa fa-plus"></i> 添加</a>
</form>

      <table id="listTable">
	  </table>
    </div>
  </div>
</div>
</div>
{/block}
{block name="script"}
<script src="{$cdnpublic}layer/3.1.1/layer.js"></script>
<script src="{$cdnpublic}bootstrap-table/1.21.4/bootstrap-table.min.js"></script>
<script src="{$cdnpublic}bootstrap-table/1.21.4/extensions/page-jump-to/bootstrap-table-page-jump-to.min.js"></script>
<script src="/static/js/custom.js"></script>
<script>
$(document).ready(function(){
	updateToolbar();
	const defaultPageSize = 15;
	const pageNumber = typeof window.$_GET['pageNumber'] != 'undefined' ? parseInt(window.$_GET['pageNumber']) : 1;
	const pageSize = typeof window.$_GET['pageSize'] != 'undefined' ? parseInt(window.$_GET['pageSize']) : defaultPageSize;

	$("#listTable").bootstrapTable({
		url: '/dmonitor/task/data',
		pageNumber: pageNumber,
		pageSize: pageSize,
		classes: 'table table-striped table-hover table-bordered',
		columns: [
			{
				field: 'id',
				title: 'ID'
			},
			{
				field: 'rr',
				title: '域名',
				formatter: function(value, row, index) {
					return '<span title="'+row.remark+'" data-toggle="tooltip" data-placement="right" title="Tooltip on right">' + value + '.' + row.domain + '</span>';
				}
			},
			{
				field: 'main_value',
				title: '解析记录',
				formatter: function(value, row, index) {
					return value;
				}
			},
			{
				field: 'type',
				title: '切换设置',
				formatter: function(value, row, index) {
					if(value == 1) {
						return '暂停解析';
					} else if(value == 2) {
						return '<span title="" data-toggle="tooltip" data-placement="bottom" data-original-title="备用：'+row.backup_value+'" class="tips">切换备用</span>';
					} else if(value == 3) {
						return '<span title="" data-toggle="tooltip" data-placement="bottom" data-original-title="同域名正常数量<='+row.cycle+'" class="tips">条件开启</span>';
					} else {
						return '无操作';
					}
				}
			},
			{
				field: 'frequency',
				title: '检测间隔',
				formatter: function(value, row, index) {
					if(row.type <= 2){
						var checktype = 'PING';
						if(row.checktype == 2){
							checktype = row.checkurl;
						}else if(row.checktype == 1){
							checktype = 'TCP('+row.tcpport+'端口)';
						}
					}else{
						var checktype = '';
					}
					return '<span title="" data-toggle="tooltip" data-placement="bottom" data-original-title="'+checktype+'" class="tips">' + value + '秒</span>';
				}
			},
			{
				field: 'status',
				title: '健康状况',
				formatter: function(value, row, index) {
					if(value == 0) {
						return '<span class="label label-success">正常</span>';
					} else {
						return '<span class="label label-danger">异常</span>';
					}
				}
			},
			{
				field: 'active',
				title: '运行开关',
				formatter: function(value, row, index) {
					var html = '';
					if(value == 1) {
						html += '<span class="btn btn-success btn-xs" onclick="setActive('+row.id+', 0)">开启</span>';
					} else {
						html += '<span class="btn btn-warning btn-xs" onclick="setActive('+row.id+', 1)">暂停</span>';
					}
					return html;
				}
			},
			{
				field: 'checktime',
				title: '上次检测时间',
				formatter: function(value, row, index) {
					return value > 0 ? row.checktimestr : '未运行';
				}
			},
			{
				field: '',
				title: '操作',
				formatter: function(value, row, index) {
					var html = '<a href="/dmonitor/task/info/'+row.id+'" class="btn btn-info btn-xs">切换日志</a>&nbsp;&nbsp;';
					html += '<a href="/dmonitor/task/edit?id='+row.id+'" class="btn btn-primary btn-xs">修改</a>&nbsp;&nbsp;';
					html += '<a href="/record/'+row.did+'?keyword='+row.rr+'" class="btn btn-default btn-xs" target="_blank">解析</a>&nbsp;&nbsp;';
					html += '<a href="javascript:delItem(\''+row.id+'\')" class="btn btn-danger btn-xs">删除</a>&nbsp;&nbsp;';
					return html;
				}
			},
		],
		onLoadSuccess: function(data) {
			$('[data-toggle="tooltip"]').tooltip()
		}
	})
})
function setActive(id, active){
	$.post('/dmonitor/task/setactive', {id: id, active: active}, function(data){
		if(data.code == 0) {
			layer.msg('修改成功', {icon: 1, time:800});
			$('#listTable').bootstrapTable('refresh');
		} else {
			layer.msg(data.msg, {icon: 2});
		}
	}, 'json');
}
function delItem(id){
	layer.confirm('确定要删除此切换策略吗？', {
		btn: ['确定','取消']
	}, function(){
		$.post('/dmonitor/task/del', {id: id}, function(data){
			if(data.code == 0) {
				layer.msg('删除成功', {icon: 1, time:800});
				$('#listTable').bootstrapTable('refresh');
			} else {
				layer.msg(data.msg, {icon: 2});
			}
		}, 'json');
	});
}
</script>
{/block}